﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using HWR_Ants.Classes;
using System.Drawing;

namespace HWR_Ants.Tools
{
    public static class Tools
    {
        public static string NL = System.Environment.NewLine;

        public static string OPTIONSFILE = "options.xml";

        public static string DEFAULTOPTIONSFILE = "defaultOptions.xml";

        public static string SAVEDTSPFILE = String.Empty;

        public static string[] ToSeperatedString(this string s, string sep)
        {
            string[] array = new string[] { sep };
            return s.Split(array, StringSplitOptions.RemoveEmptyEntries);
        }

        public static void writeXML<T>(T objectToSerialize, string fileName)
        {
            XmlSerializer ser = new XmlSerializer(typeof(T));
            FileStream fs = new FileStream(fileName, FileMode.Create);
            ser.Serialize(fs, objectToSerialize);
            fs.Close();
        }

        public static T readXML<T>(string fileName)
        {
            T ret;
            XmlSerializer ser = new XmlSerializer(typeof(T));
            StreamReader sr = new StreamReader(fileName);
            ret = (T)ser.Deserialize(sr);
            sr.Close();
            return ret;
        }

        public static Options getCurrentStoredOptions()
        {
            try
            {
                return readXML<Options>(OPTIONSFILE);
            }
            catch
            {
                try
                {
                    Tools.writeXML<Options>(new Options(), OPTIONSFILE);
                    Tools.writeXML<Options>(new Options(), DEFAULTOPTIONSFILE);
                    return readXML<Options>(OPTIONSFILE);
                }
                catch
                {
                    return null;
                }
            }
        }

        private static double[,] probabilityTable = null;

        private static Options activeCurrentOptions = null;

        public static Options ActiveCurrentOptions
        {
            get { return Tools.activeCurrentOptions; }
            set { Tools.activeCurrentOptions = value; }
        }

        public static double[,] ProbabilityTable
        {
            get { return Tools.probabilityTable; }
            set { Tools.probabilityTable = value; }
        }

        public static Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);
           
            if (destWidth == 0)
                destWidth = 1;
            if (destHeight == 0)
                destHeight = 1;


            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }

        public static string fillString(int length, string fillSign)
        {
            string ret = String.Empty;
            for (int i = 0; i < length; i++)
            {
                ret += fillSign;
            }
            return ret;
        }
    }
}
